package com.minelx.datamicroscope.core.values;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.gitee.minelx.commontools.Files;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class VariableGroups {

	private final JSONArray data;

	private VariableGroups(JSONArray data) {
		this.data = data;
	}

	public VariableGroup of(String groupName) {
		return data.toJavaList(JSONObject.class).stream()
				.map(VariableGroup::from)
				.filter(which -> which.groupName().equals(groupName))
				.findFirst().orElseThrow(() -> new IllegalArgumentException("can't find variable group named: " + groupName));
	}

	public static VariableGroups configured() {
		String content = Files.toString(Objects.requireNonNull(VariableGroups.class.getResourceAsStream("/variable_groups.json")), StandardCharsets.UTF_8);
		return new VariableGroups(JSON.parseArray(content));
	}
}
